package com.largeOrange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.largeOrange.common.R;
import com.largeOrange.dto.SetMealDto;
import com.largeOrange.pojo.Category;
import com.largeOrange.pojo.Setmeal;
import com.largeOrange.pojo.SetmealDish;
import com.largeOrange.service.CategoryService;
import com.largeOrange.service.SetMealDishService;
import com.largeOrange.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 大橙子
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetMealDishService setMealDishService;

    /**
     * 套餐管理分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,String name){
        log.info(name+"=======????????");
        Page<Setmeal> setMealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wr = new LambdaQueryWrapper<>();
        wr.like(name!=null,Setmeal::getName,name);
        wr.orderByAsc(Setmeal::getCreateTime);
        //分页查询
        setMealService.page(setMealPage,wr);
        //获取分页主要数据
        List<Setmeal> records = setMealPage.getRecords();
        //创建dto分页对象
        Page<SetMealDto> dtoPage = new Page<>();
        //对象拷贝
        BeanUtils.copyProperties(setMealPage,dtoPage,"records");
        ArrayList<SetMealDto> setMealDtos = new ArrayList<>();
        records.forEach(record->{
            SetMealDto setMealDto = new SetMealDto();
            BeanUtils.copyProperties(record,setMealDto);
            LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Category::getId,record.getCategoryId());
            Category category = categoryService.getOne(wrapper);
            setMealDto.setCategoryName(category.getName());
            setMealDtos.add(setMealDto);
        });
        dtoPage.setRecords(setMealDtos);
        return R.success(dtoPage);
    }

    /**
     * 修改套餐数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetMealDto> appearDate(@PathVariable Long id){
        log.info("========>"+id);
        Setmeal setmeal = setMealService.getById(id);
        SetMealDto setMealDto = new SetMealDto();
        BeanUtils.copyProperties(setmeal,setMealDto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setMealDishes = setMealDishService.list(wrapper);
        setMealDto.setSetmealDishes(setMealDishes);
        return R.success(setMealDto);
    }

    /**
     * 新增套餐保存
     * @param setMealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetMealDto setMealDto){
        setMealService.saveSetMealAndDish(setMealDto);
        return R.success("保存成功！");
    }

    /**
     * 套餐修改
     * @param setMealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetMealDto setMealDto){
        setMealService.updateTwoForms(setMealDto);
        return R.success("修改成功！");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAll(Long[] ids){
        setMealService.deleteAllByIds(ids);
        return R.success("删除成功！");
    }

    /**
     * 批量起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> getStatus(Long[] ids){
        for (Long id : ids) {
            Setmeal setmeal = setMealService.getById(id);
            setmeal.setStatus(1L);
            LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Setmeal::getId,id);
            setMealService.update(setmeal,wrapper);
        }
        return R.success("起售成功！");
    }
    /**
     * 批量停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> stopStatus(Long[] ids){
        for (Long id : ids) {
            Setmeal setmeal = setMealService.getById(id);
            setmeal.setStatus(0L);
            LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Setmeal::getId,id);
            setMealService.update(setmeal,wrapper);
        }
        return R.success("停售成功！");
    }
}
